package htmlparser;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.htmlparser.Node;
import org.htmlparser.Parser;
import org.htmlparser.util.NodeList;
import org.htmlparser.util.ParserException;


/**
 *
 * @author ADANIYA
 */
public class HtmlParser {
    static private final String newline = "\n";
    private Parser parser;
    private NodeList nodeList;
    private NodeList nodeParagraphList, nodeParagraphAuthorList, nodeText;
    int contador = 0;
    int contadorAutor = 0;
    String string = "";

    public HtmlParser() {        
    }

    public HtmlParser(String url) throws ParserException{
        System.out.println("Construtor HtmlParser");
        parser = new Parser(url);
        nodeList = parser.parse(null);
        nodeList.toHtml();
        nodeParagraphList = new NodeList();
        nodeText = new NodeList();
    }

    public void percorrerHtml(NodeList nodeList){
        String string;
        System.out.println("PECORRENDO ARVORE!");

        if(nodeList.size() > 0){
            System.out.println("SIZE > 0 - PERCORRENDO A LISTA");
            for (int i = 0; i < nodeList.size(); i++) {
                    Node node = nodeList.elementAt(i);
//                    System.out.println(node.toPlainTextString()); //Textos contidos nos nós.
                    string = node.toString();
                    System.out.println(node.toString());// - End (176052[289,5878],176061[289,5887]): /option
//                    System.out.println(node.getText());// - Texto dentro do nó
                }
        }else{
            System.out.println("SIZE < 0 - NÃO HA NOS NA LISTA");
        }
    }

    public void percorrerAllHtml(NodeList nodeList){
        boolean status = false;
        if (nodeList.size() > 0) {
//            System.out.println("SIZE = " + nodeList.size());
            status = true;
            for (int i = 0; i < nodeList.size(); i++) {
                Node node = nodeList.elementAt(i);
//                System.out.println(node.toPlainTextString());
                if(node.getClass().toString().contains("TextNode")){
                    string = string + newline + node.toPlainTextString();
//                    System.out.println(node.toPlainTextString());
//                    System.out.println("TextNode - " + node.getClass().toString());
                    nodeText.add(node);
                    contador++;
                }
                if(node.getText().contains("p")){
                    //System.out.println("PARAGRAFO!");                    
                    //nodeParagraphList.add(node);
                }
                if(node.getText().contains("author")){
                    //System.out.println("PARAGRAFO AUTHOR!");
                    contadorAutor++;
                    //nodeParagraphAuthorList.add(node);
                }
                //System.out.println(node.getText());
                if(node.getChildren() != null){
                    percorrerAllHtml(node.getChildren());
                }
            }
        }
    } 

    public void percorrerAllAuthorTagHtml(NodeList nodeList){
        boolean status = false;
        if (nodeList.size() > 0) {
//            System.out.println("SIZE = " + nodeList.size());
            status = true;
            for (int i = 0; i < nodeList.size(); i++) {
                Node node = nodeList.elementAt(i);
//                System.out.println(node.toPlainTextString());                
                if(node.getText().contains("author")){                        
                        string = string + newline + node.toPlainTextString();
                        nodeText.add(node);
                        contador++;
                        contadorAutor++;                    
                }
                if(node.getChildren() != null){
                    percorrerAllAuthorTagHtml(node.getChildren());
                }
            }
        }
    }
/*
    public String percorrerNosParagrafoHtml(NodeList nodeList) {
        String string = "";
        System.out.println("PECORRENDO NOS PARAGRAFOS!");

        if (nodeList.size() > 0) {
            System.out.println("SIZE > 0 - PERCORRENDO A LISTA");
            for (int i = 0; i < nodeList.size(); i++) {
                Node node = nodeList.elementAt(i);
                //System.out.println(node.toPlainTextString()); //Textos contidos nos nós.
                string = node.toString();
                if (string.contains("p")) {
                    System.out.println("Contem paragrafo");
                    string = node.toPlainTextString();
                } else {
                    System.out.println("Não Contem paragrafo");
                    string = "NADA";
                }
//                    System.out.println(node.toString());// - End (176052[289,5878],176061[289,5887]): /option
//                    System.out.println(node.getText());// - Texto dentro do nó
            }
        } else {
            System.out.println("SIZE < 0 - NÃO HA NOS NA LISTA");
            string = "";
        }
        return string;
    }
  */

    public static String convertStreamToString(InputStream is) {

        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        StringBuilder sb = new StringBuilder();
        String line = null;

        try {
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }

        } catch (IOException e) {
            e.printStackTrace();

        } finally {

            try {
                is.close();

            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return sb.toString();
    }

    public NodeList getNodeList() {
        return nodeList;
    }

    public void setNodeList(NodeList nodeList) {
        this.nodeList = nodeList;
    }

    public Parser getParser() {
        return parser;
    }

    public void setParser(Parser parser) {
        this.parser = parser;
    }

    public int getContador() {
        return contador;
    }

    public void setContador(int contador) {
        this.contador = contador;
    }

    public int getContadorAutor() {
        return contadorAutor;
    }

    public void setContadorAutor(int contadorAutor) {
        this.contadorAutor = contadorAutor;
    }

    public NodeList getNodeParagraphList() {
        return nodeParagraphList;
    }

    public void setNodeParagraphList(NodeList nodeParagraphList) {
        this.nodeParagraphList = nodeParagraphList;
    }

    public NodeList getNodeParagraphAuthorList() {
        return nodeParagraphAuthorList;
    }

    public void setNodeParagraphAuthorList(NodeList nodeParagraphAuthorList) {
        this.nodeParagraphAuthorList = nodeParagraphAuthorList;
    }

    public NodeList getNodeText() {
        return nodeText;
    }

    public void setNodeText(NodeList nodeText) {
        this.nodeText = nodeText;
    }

    public String getString() {
        return string;
    }

    public void setString(String string) {
        this.string = string;
    }

}
